// pages/addAddress/addAddress.js
const app = getApp()
var wh = require("../../utils/wh.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    themeImg: [],
    navImg: [{
        id: 1,
        img: '../../img/sj.png',
        title: '商家'
      },
      {
        id: 2,
        img: '../../img/kj.png',
        title: '砍价'
      },
      {
        id: 3,
        img: '../../img/qg.png',
        title: '抢购'
      },
      {
        id: 4,
        img: '../../img/pt.png',
        title: '拼团'
      },
      {
        id: 5,
        img: '../../img/hd.png',
        title: '活动'
      },
    ],
    choicenessImg: [],
    bargainImg: [],
    seckill: [],
    couponlist: [],
    grouplist: [],
    headlineList: [],
    general: [],
    modal: false,
    setting:'',
    swiperIndex: 0 //这里不写第一次启动展示的时候会有问题
  },
  // 轮播跳转
  swiper(e){
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
  // 是否显示我也要做小程序
  setting(){
    let that = this;
    wh.Post('/api/setting/get', {}, function (res) {
      console.log('setting',res)
      that.setData({
        setting: res
      })
    });
  },
  // 搜索
  toseachs() {
    wx.navigateTo({
      url: '../search/search',
    })
  },
  // 轮播 
  bindchange(e) {
    this.setData({
      swiperIndex: e.detail.current
    })
  },
  // 关闭模态框
  inxclose() {
    this.setData({
      modal: true
    })
  },
  // 跳转按钮
  navTo(e) {
    let idx = e.currentTarget.dataset.index
    if (idx == 0) {
      wx.switchTab({
        url: '../shop/shop'
      })
    } else if (idx == 1) {
      wx.navigateTo({
        url: '../bargain/bargain'
      })
    } else if (idx == 2) {
      wx.navigateTo({
        url: '../seckill/seckill'
      })
    } else if (idx == 3) {
      wx.navigateTo({
        url: '../group/group'
      })
    } else if (idx == 4) {
      wx: wx.navigateTo({
        url: '../activities/activities',
      })
    }

  },
  moreSeckill() {
    wx.navigateTo({
      url: '../seckill/seckill'
    })
  },
  morecom() {
    wx.navigateTo({
      url: '../commodity/commodity'
    })
  },
  moreGroup() {
    wx.navigateTo({
      url: '../group/group'
    })
  },
  moreBargain() {
    wx.navigateTo({
      url: '../bargain/bargain'
    })
  },
  moreSeckills() {
    wx.navigateTo({
      url: '../seckill/seckill'
    })
  },

  events(e) {
    console.log(e)
    wx.navigateTo({
      url: '../events/events?activityId=' + e.currentTarget.dataset.id,
    })
  },
  // 加载首页轮播
  ban() {
    let that = this;
    wh.Post('/api/common.banner/get_list', {}, function(res) {
      that.setData({
        imgUrls: res
      })
    });
  },
  // 获取头条状态
  headline() {
    let that = this;
    wh.Post('/api/mall.index/headline', {
      page: 1,
      page_num: 10
    }, function(res) {
      console.log('头条', res)
      that.setData({
        headlineList: res
      })
    });
  },
  // 获取专题活动列表
  special() {
    let that = this;
    wh.Post('/api/mall.activity/lists', {
      is_recom: 1
    }, function(res) {
      console.log(res)
      that.setData({
        themeImg: res
      })
    });
  },
  // 获取砍价列表
  getBarbainList() {
    let that = this;
    wh.Post('/api/mall.bargain/lists', {
      is_recom: 1
    }, function(res) {
      console.log('砍价',res)
      that.setData({
        bargainImg: res
      })
    });
  },
  // 获取普通商品列表
  getGeneralList() {
    let that = this;
    wh.Post('/api/mall.goods/lists', {
      is_recom: 1
    }, function(res) {
      console.log('普通',res)
      that.setData({
        general: res
      })
    });
  },
  // 获取拼团列表
  getGroupList() {
    let that = this;
    wh.Post('/api/mall.group/lists', {
      is_recom: 1
    }, function(res) {
      console.log('拼团',res)
      that.setData({
        grouplist: res
      })
    });
  },
  // 获取商城秒杀列表
  lists() {
    let that = this;
    wh.Post('/api/mall.seckill/lists', {
      is_recom: 1
    }, function(res) {
      console.log('秒杀列表', res)
      that.setData({
        seckill: res
      })
    });
  },
  //获取优惠券
  getCoupon: function() {
    let that = this;
    let u = wh.getUserInfo();
    if (wx.getStorageSync('coupon_show') == '' && u.id != undefined) {
      wh.Post("/api/mall.coupon/lists", {
        page: 1,
        page_num: 3,
        source: 2
      }, function(res) {
        console.log('获取优惠券', res)
        if (res.length > 0) {
          that.setData({
            modal: false
          });
          wx.setStorageSync('coupon_show', 1);
        }
        that.setData({
          couponlist: res
        });
      })
    } else {
      that.setData({
        modal: true
      });
    }
  },
  //领取优惠券
  linquCoupon: function(e) {
    let that = this;
    let coupon_id = e.currentTarget.dataset.id;
    wh.Post('/api/mall.coupon/receive', {
      coupon_id: coupon_id
    }, function(res) {
      wx.showToast({
        title: '领取成功',
        icon: 'none'
      });
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getCoupon()
    this.ban()
    this.special()
    this.getBarbainList()
    this.getGroupList()
    this.getGeneralList()
    this.lists()
    this.headline()
    this.setting()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    app.app_auth(this)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.getCoupon()
    this.ban()
    this.special()
    this.getBarbainList()
    this.getGeneralList()
    this.getGroupList()
    this.lists()
    this.headline()
    this.setting()
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})